package login;

import java.io.Serializable;
import java.util.ArrayList;

public final class UserCon {

        
        private ArrayList<User> users;
        private static UserCon instance = null;
        
        
      
        private UserCon() {
                users = new ArrayList<User>();
                /*this.createNewUser( "test", "test" );
                this.createNewUser( "test2", "test" );
                this.createNewUser( "test3", "test" );
                this.createNewUser( "test4", "test" );
                this.createNewUser( "test5", "test" );
                System.out.println( "filling database" );*/
        }
        
       
        public ArrayList<User> getUsers() {
                return this.users;
        }
        
       
        protected void setUsers( ArrayList<User> users ) {
                if( this.users.isEmpty() ) 
                        this.users = users;
                System.out.println( users.size() );
        }
        
      
        public static UserCon getInstance() {
                if( instance == null ) {
                        // Only 1 thread at the time should be able to 
                        synchronized( UserCon.class ) {
                                if( instance == null )
                                        instance = new UserCon();
                        }
                }
                return instance;
        }
        
        /* Adds a user to the Collection of users */
        private void addUser( User u ) {
                if( !this.getUsers().contains(u) )
                        this.getUsers().add(u.getUserID(), u );
        }
      
        public User getUser( String username ) {
                User user = null;
                if( !users.isEmpty() ) {
                        for( User u : this.getUsers() ) {
                                if( username.equalsIgnoreCase(u.getUsername()) )
                                        user = u;
                        }
                }
                return user;
        }
        
       
        public void createNewUser( String username, String password ) {
                if( this.getUser(username) == null ) 
                        this.addUser( new User(this.getUsers().size(), username, password) );
        }
        
}